package client.popups.appointment;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import miscellaneous.Storage;
import miscellaneous.TimeSupport;

import data.Appointment;
import data.User;
import data.room.Reservation;
import data.room.Room;

/**
 * Takes an appointment as argument and shows the values stored, and
 * makes it possible to change each one.
 * @author daniel
 */
public class AppointmentOverview extends JFrame implements PropertyChangeListener{

	private JPanel contentPane;
	private JTextField textStartTime;
	private JTextField textEndTime;
	private JTextField textStartDate;
	private JTextField textLocation;
	private JTextField textTitle;
	private JTextPane textPane;
	private JButton btnSlettAvtale;;
	private JButton btnLukkLagre;
	private JButton roomChooser;

	private Room room;	
	private Appointment appointment; 
	
	private RoomOverview roomOverviewPanel;

	/**
	 * Create the frame.
	 */
	public AppointmentOverview(Appointment app) {
		this.appointment = app;
		this.room = app.getReservation().getRoom();
		
		setIconImage(Toolkit.getDefaultToolkit().getImage("resources/icons/aCalendarIcon.png"));
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 379);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblSted = new JLabel("Sted:");
		lblSted.setBounds(201, 63, 46, 14);
		contentPane.add(lblSted);

		JLabel lblBeskrivelse = new JLabel("Beskrivelse:");
		lblBeskrivelse.setBounds(31, 120, 98, 14);
		contentPane.add(lblBeskrivelse);

		textPane = new JTextPane();
		textPane.setBounds(31, 136, 387, 178);
		contentPane.add(textPane);

		btnSlettAvtale = new JButton();
		btnSlettAvtale.setBounds(10, 317, 104, 23);
		contentPane.add(btnSlettAvtale);
		
		if(isOwner()) {
			btnSlettAvtale.setText("Slett");
			btnSlettAvtale.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					deleteAppointment();
				}
			});
		
		} else {
			btnSlettAvtale.setText("Avslå");
			btnSlettAvtale.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, "Mangler kode for å sende avslag");

				}
			});
		}

		btnLukkLagre = new JButton();
		btnLukkLagre.setBounds(158, 317, 142, 23);
		contentPane.add(btnLukkLagre);

		if(isOwner()) {
			btnLukkLagre.setText("Lagre og lukk");
			btnLukkLagre.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String datestart = textStartTime.getText() + ":00 " + textStartDate.getText();
					String dateend = textEndTime.getText() + ":00 " + textStartDate.getText();
					Calendar gcStart = TimeSupport.convertStringDateToCalendar(datestart);
					Calendar gcEnd = TimeSupport.convertStringDateToCalendar(dateend);
					if(gcStart.getTimeInMillis() > gcEnd.getTimeInMillis() || 
							gcEnd.getTimeInMillis() < gcStart.getTimeInMillis()){
						JOptionPane.showMessageDialog(null, "Ugyldig tidspunkt");
					}else{
						updateObjectFromGUI();
						closeWindow();
					}
				}
			});

		} else {
			btnLukkLagre.setText("Lukk");
			btnLukkLagre.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					closeWindow();	
				}
			});
		}

		roomChooser = new JButton("Velg rom");
		roomChooser.setBounds(79, 58, 117, 25);
		contentPane.add(roomChooser);

		roomChooser.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				openRoomSelectioner();
			}
		});

		JLabel lblRom = new JLabel("Rom");
		lblRom.setBounds(31, 63, 49, 15);
		contentPane.add(lblRom);

		JLabel lblStart = new JLabel("Start:");
		lblStart.setBounds(31, 2, 59, 14);
		contentPane.add(lblStart);

		JLabel lblSlutt = new JLabel("Slutt:");
		lblSlutt.setBounds(31, 32, 59, 14);
		contentPane.add(lblSlutt);

		JLabel lblTittel = new JLabel("Tittel:");
		lblTittel.setBounds(32, 94, 59, 14);
		contentPane.add(lblTittel);

		textStartTime = new JTextField();
		textStartTime.setBounds(99, 0, 63, 23);
		contentPane.add(textStartTime);
		textStartTime.setColumns(10);

		textEndTime = new JTextField();
		textEndTime.setBounds(99, 26, 63, 25);
		contentPane.add(textEndTime);
		textEndTime.setColumns(10);

		textStartDate = new JTextField();
		textStartDate.setBounds(249, 11, 92, 23);
		contentPane.add(textStartDate);
		textStartDate.setColumns(10);

		textLocation = new JTextField();
		textLocation.setBounds(249, 61, 169, 25);
		contentPane.add(textLocation);
		textLocation.setColumns(10);

		textTitle = new JTextField();
		textTitle.setBounds(79, 92, 339, 23);
		contentPane.add(textTitle);
		textTitle.setColumns(10);
		
		JLabel lblDato = new JLabel("Dato:");
		lblDato.setBounds(189, 15, 46, 14);
		contentPane.add(lblDato);

		if(!isOwner()) {
			textStartTime.setEditable(false);
			textEndTime.setEditable(false);
			textStartDate.setEditable(false);
//			textEndDate.setEditable(false);
			textLocation.setEditable(false);
			textTitle.setEditable(false);
			textPane.setEditable(false);
			roomChooser.setEnabled(false);
		}
		
		setValuesToGUI();
	}

	/**
	 * Sets the correct value to the GUI (fields) based on the object(s).
	 */
	public void setValuesToGUI() {
		if(appointment != null) {
			if(appointment.getReservation().getStartTime() != null) {
				textStartTime.setText(TimeSupport.showTimeNice(
						appointment.getReservation().getStartTime()));
				textStartDate.setText(TimeSupport.showDateNice(
						appointment.getReservation().getStartTime()));	
			}
			if(appointment.getReservation().getEndTime() != null) {
				textEndTime.setText(TimeSupport.showTimeNice(
						appointment.getReservation().getEndTime()));
//				textEndDate.setText(TimeSupport.showDateNice(
//						appointment.getReservation().getEndTime()));
			}
			if(appointment.getTitle() != null) {
				textTitle.setText(appointment.getTitle());
			}
			if(appointment.getDescription() != null) {
				textPane.setText(appointment.getDescription());
			}
			if(appointment.getReservation().getRoom() != null) {
				roomChooser.setText(appointment.getReservation().
						getRoom().getName());
				textLocation.setText(appointment.getReservation().
						getRoom().getCasualName());
				textLocation.setEnabled(false);
			} else {
				if(appointment.getLocation() != null) {
					textLocation.setText(appointment.getLocation());
					textLocation.setEnabled(true);

				}
			}
		}
	}

	/**
	 * Updates the object with the values from GUI
	 */
	public void updateObjectFromGUI() {
		String datestart = textStartTime.getText() + ":00 " + textStartDate.getText();
		String dateend = textEndTime.getText() + ":00 " + textStartDate.getText();

		appointment.getReservation().updateAll(
				TimeSupport.convertStringDateToCalendar(datestart),
				TimeSupport.convertStringDateToCalendar(dateend), 
				room);

		appointment.UpdateAll(
				textTitle.getText(), textPane.getText(),
				textLocation.getText(), appointment.getReservation());

		JOptionPane.showMessageDialog(this, "Lagret");
	}

	/**
	 * Opens the a GUI where it is possible to select a available room
	 */
	public void openRoomSelectioner() {

		if(this.room != null){
			roomOverviewPanel = new RoomOverview(this.room, appointment);		//send inn the current appointment to room selector
		} else {
			roomOverviewPanel = new RoomOverview(null, appointment);
		}
		roomOverviewPanel.addPropertyChangeListener(this);
		roomOverviewPanel.pack();
		roomOverviewPanel.setVisible(true);
		roomOverviewPanel.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	public Room getRoom() {
		return room;
	}

	public void deleteAppointment() {
		Storage.appointments.remove(appointment);
		this.dispose();
	}

	/**
	 * Closes the window
	 */
	public void closeWindow() {
		this.dispose();
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	private boolean isOwner() {
		return Storage.getloggedInUser() == appointment.getOwner();
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Room room = new Room();
					room.setName("P15 - 411");
					Calendar cal = Calendar.getInstance();
					cal.add(Calendar.MINUTE, 60);
					room.setCasualName("Datarom 4.etg");
					AppointmentOverview frame = new AppointmentOverview(new Appointment(
							"SCRUMmøte", "For gruppe 2", "P15, rom 411", (
									new User()), new Reservation(
											Calendar.getInstance(),
											cal,
											room)));
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getPropertyName().equals("overviewUpdate")){
			this.room = (Room) evt.getNewValue();
			if(this.room != null){
				roomChooser.setText(this.room.getName());
				textLocation.setText(this.room.getCasualName());
				textLocation.setEnabled(false);
			} else {
				roomChooser.setText("");
				textLocation.setText("");
				textLocation.setEnabled(true);
			}
			roomOverviewPanel.removePropertyChangeListener(this);
		}
	}
}